package org.jdsm.flow.core.users.impl.jpa;


import org.jdsm.flow.core.users.IRole;

import javax.persistence.*;
import java.util.Set;

/**
 * User: Michael Komarichin aka mikom
 * Date: 13.06.2010
 * Time: 13:49:19
 */
@Entity
@Table(name = "WF_USERS_ROLE")
@NamedQueries(
        {
                @NamedQuery(name = "role.findRoleByName", query = "SELECT r FROM Role r WHERE r.name = :roleName"),
                @NamedQuery(name = "role.findAllowRoles", query = "SELECT r FROM Role r WHERE r.permission > :rolePermission"),
                @NamedQuery(name = "role.findUsers", query = "SELECT r.users FROM Role r WHERE r = :role"),
                @NamedQuery(name = "role.findRolesByPermission", query = "SELECT r FROM Role r WHERE r.permission = :rolePermission")
        }
)
public class Role implements IRole<Long> {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name = "ID")
    private Long id;
    @Column(name = "NAME", length = 255, nullable = false, unique = true)
    private String name;
    @Column(name = "PERMISSION")
    private Long permission;
    @Column(name = "DESCRIPTION")
    private String description;

    @ManyToMany(targetEntity = User.class)
    @JoinTable(name = "WF_TBL_USER_ROLE",
            joinColumns = @JoinColumn(name = "RID", referencedColumnName = "ID"),
            inverseJoinColumns = @JoinColumn(name = "UID", referencedColumnName = "ID"))
    private Set<User> users;

    @Override
    public Long getId() {
        return id;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public Long getPermission() {
        return permission;
    }

    @Override
    public String getDescription() {
        return description;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPermission(Long permission) {
        this.permission = permission;
    }

    public void setDescription(String description) {
        this.description = description;
    }


    public Set<User> getUsers() {
        return users;
    }

    public void setUsers(Set<User> users) {
        this.users = users;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Role role = (Role) o;

        if (description != null ? !description.equals(role.description) : role.description != null) return false;
        if (id != null ? !id.equals(role.id) : role.id != null) return false;
        if (name != null ? !name.equals(role.name) : role.name != null) return false;
        if (permission != null ? !permission.equals(role.permission) : role.permission != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id != null ? id.hashCode() : 0;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (permission != null ? permission.hashCode() : 0);
        result = 31 * result + (description != null ? description.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("Role");
        sb.append("{id=").append(id);
        sb.append(", name='").append(name).append('\'');
        sb.append(", permission=").append(permission);
        sb.append(", description='").append(description).append('\'');
        sb.append('}');
        return sb.toString();
    }
}
